package com.ivo.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.util.EntityUtils;

public class HttpRequestUtil {
	
	protected static Log log = LogFactory.getLog( HttpRequestUtil.class );
    /** 
     * 发送 get请求 
     */  
    public static void get(String url) {  
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			// 创建httpget. 
			HttpGet httpget = new HttpGet(url);
			System.out.println("executing request " + httpget.getURI());
			// 执行get请求. 
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体 
				HttpEntity entity = response.getEntity();
				System.out.println("--------------------------------------");
				// 打印响应状态
				System.out.println(response.getStatusLine());
				if (entity != null) {
					// 打印响应内容长度  
					System.out.println("Response content length: " + entity.getContentLength());
					// 打印响应内容  
					System.out.println("Response content: " + EntityUtils.toString(entity));
				}
				System.out.println("------------------------------------");
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (IllegalArgumentException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源  
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    

    /** 
     *发送 post请求 
     */  
    public static String post(String url,List<NameValuePair> params) {  
    	
    	String result= "";
    		// 创建默认的httpClient实例. 
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost 
		HttpPost httppost = new HttpPost(url);
		// 创建参数队列 
//		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//		formparams.add(new BasicNameValuePair("eid", "C0902600"));
		UrlEncodedFormEntity uefEntity;
		try {
			uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
			httppost.setEntity(uefEntity);
			//System.out.println("executing request " + httppost.getURI());
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					result = EntityUtils.toString(entity, "UTF-8");
					System.out.println("--------------------------------------");
					//System.out.println("Response content: " + result);
					System.out.println("--------------------------------------");
					
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源 
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;    	
    }
    public static void getHttpAsyncClient(String url) throws IOException {
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        try {
            httpclient.start();
            HttpGet request = new HttpGet(url);
            Future<HttpResponse> future = httpclient.execute(request, null);
            HttpResponse response = future.get();
            System.out.println("Response: " + response.getStatusLine());
            System.out.println("Shutting down");
        } catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源 
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        System.out.println("Done");
    }
    public static void postHttpAsyncClient(String url,List<NameValuePair> params) throws IOException {
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        UrlEncodedFormEntity uefEntity;
        try {
        	log.info("Response start: "+url);
            httpclient.start();
            HttpPost request = new HttpPost(url);
            uefEntity = new UrlEncodedFormEntity(params, "UTF-8");
            request.setEntity(uefEntity);
            Future<HttpResponse> future = httpclient.execute(request, null);
            HttpResponse response = future.get();
            log.info("Response: " + response.getStatusLine());
            log.info("Shutting down");
        } catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源 
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        System.out.println("Done");
    }
}
